package com.ad.pet.ui.loopview;

import android.content.Context;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

public class MyViewPager extends ViewPager {

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewPager(Context context) {
        super(context);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = 0;
        int with=0;
        float scale=1;
        //下面遍历所有child的高度，获得子控件的最大宽和高
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            //自己测量宽高
            child.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            int w=child.getMeasuredWidth();
            if(w>with)//采用最大的view的宽度
                with=w;
            if (h>height) //采用最大的view的高度。
                height = h;
        }

        if(with>0){
            //获得屏幕的宽度
            int dWidth=((WindowManager)this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
            //得到屏幕宽和图片宽的比例
            scale=(float)dWidth/(float)with;
            //根据上边的比例来求出ViewPager应该有的高度
            heightMeasureSpec= MeasureSpec.makeMeasureSpec((int)(height*scale),
                    MeasureSpec.EXACTLY);
            //ViewPager的宽为屏幕的宽度
            widthMeasureSpec= MeasureSpec.makeMeasureSpec(dWidth,
                    MeasureSpec.EXACTLY);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private int downx;
    private int downy;
    private long startTime;
    private long endTime;
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //自定义点击事件
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startTime= SystemClock.uptimeMillis();
                downx=(int) ev.getX();
                downy=(int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                endTime= SystemClock.uptimeMillis();
                int touchx=(int) ev.getX()-downx;
                int touchy=(int) ev.getY()-downy;
                //500毫秒内触摸屏幕的像素不大于20则认为是点击
                if(Math.sqrt(touchx*touchx+touchy*touchy)<20&&(endTime-startTime)<500){
                    //处理点击事件
                    if(listener!=null){
                        listener.onclick(getCurrentItem());
                    }
                }
                break;

        }
        return super.onTouchEvent(ev);
    }

    //条目点击监听器接口
    public interface MItemOnclickListener{
        public void onclick(int position);
    }

    private MItemOnclickListener listener;
    //设置条目点击监听器
    public void setOnItemClickListener(MItemOnclickListener listener){
        this.listener=listener;
    }

}